package vista;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import modelo.Ciudad;
import modelo.DetalleSolicitud;
import modelo.Solicitud;
import modelo.Status;
import modelo.Sucursal;
import modelo.dao.Servidor;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class DlgSeleccionarDetalleSolicitud extends javax.swing.JDialog {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JTextField txtCodigo;
	private JTextField txtCliente;
	private JLabel lblCliente;
	private JScrollPane jScrollPane1;
	private JButton btAceptar;
	private JButton btSalir;
	private JTable tbDetalles;
	private JLabel jLabel2;
	private JLabel jLabel1;
	private JTextField txtSucursal;
	private JLabel lblCodigo;
    private Solicitud solicitud=null;
    private ArrayList<DetalleSolicitud> detalles_sol;
	private AbstractTableModel tbModelDetalles;
	/**
	* Auto-generated main method to display this JDialog
	*/
	/*public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame();
				DlgSeleccionarDetalleSolicitud inst = new DlgSeleccionarDetalleSolicitud(frame);
				inst.setVisible(true);
			}
		});
	}*/
	
	public DlgSeleccionarDetalleSolicitud(JDialog dialog,Solicitud sol) {
		super(dialog);
		solicitud=sol;
		initGUI();
	}
	
	private void initGUI() {
		try {
			{
				getContentPane().setLayout(null);
				this.setTitle("Seleccionar Detalle de Solicitud");
				{
					txtCodigo = new JTextField();
					getContentPane().add(txtCodigo);
					txtCodigo.setBounds(182, 19, 88, 28);
					txtCodigo.setEnabled(false);
				}
				{
					lblCodigo = new JLabel();
					getContentPane().add(lblCodigo);
					lblCodigo.setText("Numero de Solicitud:");
					lblCodigo.setFont(new java.awt.Font("Tahoma",1,11));
					lblCodigo.setBounds(33, 22, 143, 14);
				}
				{
					lblCliente = new JLabel();
					getContentPane().add(lblCliente);
					lblCliente.setText("Cliente:");
					lblCliente.setFont(new java.awt.Font("Tahoma",1,11));
					lblCliente.setBounds(287, 26, 52, 14);
				}
				{
					txtCliente = new JTextField();
					getContentPane().add(txtCliente);
					txtCliente.setBounds(351, 19, 190, 28);
					txtCliente.setEnabled(false);
				}
				{
					txtSucursal = new JTextField();
					getContentPane().add(txtSucursal);
					txtSucursal.setBounds(104, 57, 437, 28);
					txtSucursal.setEnabled(false);
				}
				{
					jLabel1 = new JLabel();
					getContentPane().add(jLabel1);
					jLabel1.setText("Sucursal:");
					jLabel1.setFont(new java.awt.Font("Tahoma",1,11));
					jLabel1.setBounds(33, 64, 71, 14);
				}
				{
					jLabel2 = new JLabel();
					getContentPane().add(jLabel2);
					jLabel2.setText("Detalles de la Solicitud:");
					jLabel2.setFont(new java.awt.Font("Tahoma",1,11));
					jLabel2.setBounds(33, 107, 155, 14);
				}
				{
					jScrollPane1 = new JScrollPane();
					getContentPane().add(jScrollPane1);
					jScrollPane1.setBounds(33, 145, 486, 116);
					{
						detalles_sol = Servidor.solicitudes().consultarDetallePorSolicitudYStatus(solicitud.getCodigo(), Status.ACTIVO);
						
						tbModelDetalles = new AbstractTableModel() {
							@Override
							public Object getValueAt(int fil, int col) {
								switch (col) {
								case 0: return  detalles_sol.get(fil).getDescripcion();
								case 1: if(detalles_sol.get(fil).getEquipo()!=null)
									     return detalles_sol.get(fil).getEquipo().getDescripcion();
										else
											return "NO ASIGNADO";
							
								}
								
								return null;
							}
							
							@Override
							public int getRowCount() {
								return detalles_sol.size();
							}
							
							@Override
							public String getColumnName(int col) {
								switch (col) {
								case 0: return "Detalle";
								case 1: return "Equipo";
								}
								
								return null;
							}
							@Override
							public int getColumnCount() {
								return 2;
							}
						};
						tbDetalles = new JTable();
						jScrollPane1.setViewportView(tbDetalles);
						tbDetalles.setModel(tbModelDetalles);
						tbDetalles.setBounds(111, 34, 150, 32);
					}
				}
				{
					btSalir = new JButton();
					getContentPane().add(btSalir);
					btSalir.setText("Salir");
					btSalir.setIcon(new ImageIcon(getClass().getClassLoader().getResource("iconos/Salir.gif")));
					btSalir.setHorizontalTextPosition(JButton.CENTER);
					btSalir.setVerticalTextPosition(JButton.BOTTOM);
					btSalir.setBounds(462, 300, 79, 40);
				}
				{
					btAceptar = new JButton();
					getContentPane().add(btAceptar);
					btAceptar.setText("Aceptar");
					btAceptar.setIcon(new ImageIcon(getClass().getClassLoader().getResource("iconos/Aceptar.png")));
					btAceptar.setHorizontalTextPosition(JButton.CENTER);
					btAceptar.setVerticalTextPosition(JButton.BOTTOM);
					btAceptar.setBounds(355, 300, 79, 40);
				}
			}
			this.setSize(578, 399);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	DlgSeleccionarDetalleSolicitud dsdsol=this;
	   public void mostrar(JDialog dialog) {
			
			setLocationRelativeTo(null);
			llenarDatos(solicitud);
			
			btAceptar.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {

					if (tbDetalles.getSelectedRow() != -1) {
					     DlgServicioPrestado dspre = new DlgServicioPrestado(dsdsol);
					     dspre.mostrarPlanificarServicio(detalles_sol.get(tbDetalles.getSelectedRow()));
					     detalles_sol = Servidor.solicitudes().consultarDetallePorSolicitudYStatus(solicitud.getCodigo(), Status.ACTIVO);
					      tbModelDetalles.fireTableDataChanged();
					     dsdsol.dispose();
					} else

						JOptionPane.showMessageDialog(dsdsol, "Debe Seleccionar un Detalle de Solicitud", "Seleccionar",JOptionPane.ERROR_MESSAGE);
				}
			});
			
			btSalir.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					dsdsol.dispose();
				}
			});
			

			this.setModalityType(ModalityType.APPLICATION_MODAL);
			setVisible(true);
			
			
		}
	   
	   public void llenarDatos(Solicitud obj){
		   txtCodigo.setText(String.format("%04d", obj.getCodigo()));
		   txtCliente.setText(obj.getSucursal().getCliente().getNombre());
		   txtSucursal.setText(obj.getSucursal().getDescripcion());	
			
		}
	   
	 

}
